// 此处总页数与 tltArr datasource 皆为测试数据 （具体字段名称以实际为准） 用来展示表格与分页器  
// 此分页器需要配合后端接口一起使用

Page({
  data: {
    pageSize:4,   //一页多少条
    pageNo:1,
    datasource:[
      {
        "username":'猪沛购物拉',
        "draw":'1',
        "address":'广东广州黄埔区大沙东319号保利中誉2501',
        "formType":'排行榜',
        "drawType":'实物',
        "drawTime":'2021-01-19 15:56:00'
      },
      {
        "username":'猪沛购物拉',
        "draw":'2',
        "address":'广东广州黄埔区大沙东319号保利中誉2501',
        "formType":'排行榜',
        "drawType":'优惠券',
        "drawTime":'2021-01-19 15:56:00'
      },
      {
        "username":'猪沛购物拉',
        "draw":'3',
        "address":'广东广州黄埔区大沙东319号保利中誉2501',
        "formType":'排行榜',
        "drawType":'优惠券',
        "drawTime":'2021-01-19 15:56:00'
      },
      {
        "username":'猪沛购物拉',
        "draw":'4',
        "address":'广东广州黄埔区大沙东319号保利中誉2501',
        "formType":'排行榜',
        "drawType":'优惠券',
        "drawTime":'2021-01-19 15:56:00'
      },
      {
        "username":'猪沛购物拉',
        "draw":'5',
        "address":'广东广州黄埔区大沙东319号保利中誉2501',
        "formType":'排行榜',
        "drawType":'优惠券',
        "drawTime":'2021-01-19 15:56:00'
      },
      {
        "username":'猪沛购物拉',
        "draw":'6',
        "address":'广东广州黄埔区大沙东319号保利中誉2501',
        "formType":'排行榜',
        "drawType":'优惠券',
        "drawTime":'2021-01-19 15:56:00'
      }
    ],
    tltArr:[
      {title:'淘宝会员名称',id:'username'},
      {title:'奖品名称',id:'draw'},
      {title:'留资信息',id:'address'},
      {title:'发奖类型',id:'formType'},
      {title:'奖品类型',id:'drawType'},
      {title:'获奖时间',id:'drawTime'}
    ],
    datasource1:[],
  },

  onPageChange(e){
    //获取分页器当前第几页，传给后端，后端返回数据覆盖 datasource 即可  
    //console.log(e.detail.value);

    this.setData({
      pageNo:e.detail.value
    })



    this.paging(e.detail.value,this.data.pageSize);

  },

  // 分页计算封装
  paging(No,Size){
    
    let datasource = this.data.datasource
    let pageSize = Size
    let pageNo = No

    let x = datasource.length % pageSize
    
    //当前页不满足pageSize
    let z = x + ( pageNo - 1 ) * pageSize

    let dataA = [];

    for(let i = ( pageNo - 1 ) * pageSize; i< ( pageNo * pageSize > datasource.length ? z : pageNo * pageSize ); i++){

      console.log(`第${pageNo}页，每页${pageSize}的数据${datasource[i].draw}`)

      dataA.push(datasource[i])

    }

    this.setData({
      datasource1:dataA
    })

  },

  onLoad(query) {

    setTimeout(() => {
      this.paging(1,this.data.pageSize)
    }, 10);

    console.log('page onLoad', query)

  },
  onShow() {}
})